# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog, QMessageBox
import os


class ImageViewer(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.setup_ui()
        self.original_image = None
        self.fixed_image_path = "C:/test_image.jpg"  # 固定图片路径

    def setup_ui(self):
        self.setWindowTitle("图片查看器")
        self.resize(800, 600)

        # 中央部件
        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)

        # 图像显示区域
        self.image_label = QtWidgets.QLabel()
        self.image_label.setAlignment(QtCore.Qt.AlignCenter)
        self.image_label.setStyleSheet("""
            QLabel {
                background-color: #f0f0f0;
                border: 2px dashed #aaa;
                qproperty-alignment: 'AlignCenter';
            }
        """)
        self.image_label.setText("图片显示区域")

        # 按钮区域
        self.upload_btn = QtWidgets.QPushButton("上传图片")
        self.recognize_btn = QtWidgets.QPushButton("识别")

        # 布局
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.image_label, 1)

        btn_layout = QtWidgets.QHBoxLayout()
        btn_layout.addWidget(self.upload_btn)
        btn_layout.addWidget(self.recognize_btn)
        layout.addLayout(btn_layout)

        central_widget.setLayout(layout)

        # 连接信号
        self.upload_btn.clicked.connect(self.upload_image)
        self.recognize_btn.clicked.connect(self.load_fixed_image)

    def upload_image(self):
        """上传并显示图片"""
        file_name, _ = QFileDialog.getOpenFileName(
            self, "选择图片", "",
            "图片文件 (*.png *.jpg *.jpeg *.bmp)"
        )

        if file_name:
            self.original_image = QtGui.QImage(file_name)
            if self.original_image.isNull():
                QMessageBox.warning(self, "错误", "无法加载图片！")
                return

            self.display_image(self.original_image)

    def load_fixed_image(self):
        """加载固定路径图片"""
        if not os.path.exists(self.fixed_image_path):
            QMessageBox.warning(
                self, "文件不存在",
                f"固定路径图片不存在：\n{self.fixed_image_path}\n请检查路径或放置图片！"
            )
            return

        fixed_image = QtGui.QImage(self.fixed_image_path)
        if fixed_image.isNull():
            QMessageBox.warning(self, "错误", "无法加载固定路径图片！")
            return

        self.display_image(fixed_image)

    def display_image(self, image):
        """显示图片（自动缩放）"""
        scaled = image.scaled(
            self.image_label.width() - 20,
            self.image_label.height() - 20,
            QtCore.Qt.KeepAspectRatio,
            QtCore.Qt.SmoothTransformation
        )
        self.image_label.setPixmap(QtGui.QPixmap.fromImage(scaled))


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    # 检查固定路径是否存在
    fixed_path = "C:/test_image.jpg"
    if not os.path.exists(fixed_path):
        print(f"提示：请确保固定路径图片存在：{fixed_path}")

    window = ImageViewer()
    window.show()
    sys.exit(app.exec_())